package com.hzhr.iot.device.service;

import com.hzhr.iot.collect.sdk.model.command.chongqi.ChongqiModel;
import com.hzhr.iot.collect.sdk.model.command.jichao.JichaoModel;
import com.hzhr.iot.collect.sdk.model.command.searchConcentratorClock.SearchConcentratorClockModel;
import com.hzhr.iot.collect.sdk.model.command.searchHeartPeriod.SearchHeartPeriodModel;
import com.hzhr.iot.collect.sdk.model.command.setConcentratorClock.SetConcentratorClockModel;
import com.hzhr.iot.collect.sdk.model.command.setHeartPeriod.SetHeartPeriodModel;
import com.hzhr.iot.collect.sdk.service.Collect_CommandService;
import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygQuerys;
import com.hzhr.iot.core.constant.AccessModeEnum;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.device.constant.Device_ErrorsEnum;
import com.hzhr.iot.device.dao.IotDeviceDao;
import com.hzhr.iot.device.entity.CollectDeviceEntity;
import com.hzhr.iot.device.entity.IotDeviceEntity;
import com.hzhr.iot.device.model.concentrator_commandSend.Concentrator_CommandSendGetModel;
import com.hzhr.iot.device.model.concentrator_commandSend.Concentrator_CommandSendPostModel;
import com.hzhr.iot.device.model.concentrator_meter_command.Concentrator_Meter_CommandGetModel;
import com.hzhr.iot.device.model.concentrator_meter_commandSend.Concentrator_Meter_CommandSendGetModel;
import com.hzhr.iot.device.model.concentrator_meter_commandSend.Concentrator_Meter_CommandSendPostModel;
import com.hzhr.iot.device.queryModel.custom.IotDeviceCustomQueryModel;
import com.hzhr.iot.device.service.base.BaseService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.Map;

@Service
@CygQuerys({

})
public class Concentrator_CommandSendService extends BaseService {

    @Autowired
    IotDeviceDao iotDeviceDao;

    @Autowired
    Collect_CommandService collect_commandService;

    //region javadoc注释 : post
    /**
     * 向集中器推送指令 <br/><br/>
     * 步骤 : <br/>
     * 1. 查询集中器 : <br/>
     * 根据路径参数concentratorId查询IotDeviceEntity对象concentrator_1，查询规则如下： <br/>
     *           IotDeviceEntity.id = 路径参数concentratorId； <br/>
     *           IotDeviceEntity.isValid = '1'； <br/>
     * 如果concentrator_1不存在，抛出异常201； <br/>
     * 2. 发送指令 <br/>
     * 3. 返回信息 <br/>
     * 创建结果对象并返回； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE');<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易流水号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-transaction-type : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 交易类型;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-access-mode : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 访问模式 0:一般 1:全局;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 0;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _protocolId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 协议编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 2;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param concentratorId 集中器编号 <br/><br/>
     * @param body <br/><br/>
     * @throws ServiceException <br/>
     * CONCENTRATOR_COMMANDSENDSERVICE_POST_201 : <br/>&nbsp&nbsp&nbsp&nbsp异常类型 : 业务流程异常<br/>&nbsp&nbsp&nbsp&nbsp异常业务 : 查询集中器<br/>&nbsp&nbsp&nbsp&nbsp异常描述 : 集中器不存在<br/>
     * @see Concentrator_CommandSendPostModel body <br/>
     * @see Concentrator_CommandSendGetModel result <br/>
     */ 
    //endregion
    //region 说明 : post
    //向集中器推送指令
    //步骤 :
    //1. 查询集中器 :
    //根据路径参数concentratorId查询IotDeviceEntity对象concentrator_1，查询规则如下：
    //          IotDeviceEntity.id = 路径参数concentratorId；
    //          IotDeviceEntity.isValid = '1'；
    //如果concentrator_1不存在，抛出异常201；
    //2. 发送指令
    //3. 返回信息
    //创建结果对象并返回；
    //endregion
    //region 参数 : post
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "concentratorId", value = "集中器编号",required = true, dataType = "int", paramType = "path"),

            @ApiImplicitParam(name = "protocolId", value = "协议编号",required = false, defaultValue = "2", dataType = "int", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-type", value = "用户类型(关联: common_service.options.code, typeCode = 'USER_TYPE')",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-id", value = "交易流水号",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-transaction-type", value = "交易类型",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-access-mode", value = "访问模式 0:一般 1:全局",defaultValue = "0", required = false, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_protocolId", value = "协议编号",required = false, defaultValue = "2", dataType = "int", paramType = "query"),

    })
    //endregion
    //region 请求 : post
    @RequestMapping(value = "/concentrators/{concentratorId}/command-sends", method = RequestMethod.POST)
    //endregion
    public Concentrator_CommandSendGetModel post(Map<String, String> header, Integer concentratorId,
                                                 Map<String, Object> query, Concentrator_CommandSendPostModel body) throws ServiceException {
        //向集中器推送指令
        //1. 查询集中器
        IotDeviceCustomQueryModel queryModel = new IotDeviceCustomQueryModel();
        queryModel.setEqual_id(concentratorId);
        queryModel.setEqual_isValid("1");
        queryModel.setShow(true);

        IotDeviceEntity concentrator_1 = iotDeviceDao.selectExt(queryModel);
        if (concentrator_1 == null){
            throw new ServiceException(Device_ErrorsEnum.CONCENTRATOR_METER_COMMANDSENDSERVICE_POST_201);
        }
        String protocolId =concentrator_1.getProtocolId();
        if (protocolId == null) {
            protocolId = "2";
        }

        //2. 发送指令
        if (body.getType() == 1){
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            SetConcentratorClockModel setConcentratorClockModel = new SetConcentratorClockModel();
            setConcentratorClockModel.setDeviceId(iotPlatformDeviceId);
            setConcentratorClockModel.setTime(body.getTime());
            collect_commandService.ccr_setConcentratorClock(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,setConcentratorClockModel);
        }
        if (body.getType() == 2){
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            SetHeartPeriodModel setHeartPeriodModel = new SetHeartPeriodModel();
            setHeartPeriodModel.setDeviceId(iotPlatformDeviceId);
            setHeartPeriodModel.setHeartPeriod(HzhrTypeUtil.integer2string(body.getHeartPeriod()));
            collect_commandService.ccr_setHeartPeriod(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,setHeartPeriodModel);
        }
        if (body.getType() == 3){
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            SearchConcentratorClockModel searchConcentratorClockModel = new SearchConcentratorClockModel();
            searchConcentratorClockModel.setDeviceId(iotPlatformDeviceId);
            collect_commandService.ccr_searchConcentratorClock(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,searchConcentratorClockModel);
        }
        if (body.getType() == 4){
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            SearchHeartPeriodModel searchHeartPeriodModel = new SearchHeartPeriodModel();
            searchHeartPeriodModel.setDeviceId(iotPlatformDeviceId);
            collect_commandService.ccr_searchHeartPeriod(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,searchHeartPeriodModel);
        }
        if (body.getType() == 5) {
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            JichaoModel jichaoModel = new JichaoModel();
            jichaoModel.setDeviceId(iotPlatformDeviceId);
            collect_commandService.ccr_jichao(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,jichaoModel);
        }
        if (body.getType() == 6) {
            String iotPlatformDeviceId = concentrator_1.getIotPlatformDeviceId();
            ChongqiModel chongqiModel = new ChongqiModel();
            chongqiModel.setDeviceId(iotPlatformDeviceId);
            collect_commandService.ccr_chongqi(initHeader(header, AccessModeEnum.GLOBAL),Integer.parseInt(protocolId),null,chongqiModel);
        }

        Concentrator_CommandSendGetModel concentrator_commandSendGetModel = new Concentrator_CommandSendGetModel();
        return concentrator_commandSendGetModel;
    }

}

